package com.example.take_out_dish.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_dish.common.Result;
import com.example.take_out_dish.entity.Category;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.service.CategoryService;
import com.example.take_out_dish.service.DishService;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @since 2024-07-01
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Tag(name = "菜品分类")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishService dishService;

    @GetMapping("page")
    public Result<Page> page(int page, int pageSize,String name,String type) {
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(name),Category::getName,name);
        wrapper.like(StrUtil.isNotBlank(type),Category::getType,type);
        Page pageInfo = categoryService.page(new Page(page, pageSize),wrapper);
        log.info("pageInfo:{}", pageInfo);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result<Object> save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    @DeleteMapping
    public Result<Object> deleteById(String id) {
        //检查这个分类下面有没有菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        long count = dishService.count(wrapper);
        if (count > 0) {
            throw new BusinessException(ResponseEnum.CATEGORY_ERROR);
        }
        categoryService.removeById(id);
        return Result.success();
    }


    @PutMapping()
    public Result<Object> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Category>> list(int type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);
       return Result.success( categoryService.list(new Page<>(), wrapper));
    }
    @PostMapping("/status/{status}")
    public Result<Object> updatestuats(@PathVariable Integer status,@PathParam("id") String id) {
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId,id);
        Category one = categoryService.getOne(wrapper);
        one.setStatus(status);
        System.out.println("准备进行修改操作");
        categoryService.updateStatus(one);
        return Result.success();
    }
}
